const app = getApp()
const util = require("../../utils/utils.js")

// pages/forum-detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    post: {},
    comments: [],
    postUserImg: "",
    postUserName: "",
    commentsUserImg: [],
    commentsUserName: [],
    doILike: null,
    isCommentPopupShow: false,
    commentTextareaValue:"",
    loginUserInfo:{},
    userImg: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "帖子详情"
    });
    this.setData({
      userImg: app.globalData.userImg
    })
    var that = this
    that.setData({
      loginUserInfo: app.globalData.userInfo
    })
    wx.request({
      url: 'http://localhost:8888/posting/getPostingById',
      method: 'GET',
      data: {
        id: app.globalData.postingId
      },
      head: {
        'content-type': 'application/json'
      },
      success:function (res) {
        that.setData({
          post: res.data.data
        })
        that.getPostHeadImg(res.data.data.username)
        that.setData({
          "post.photo": util.strToArr(res.data.data.photo),
          "post.tag": util.strToArr(res.data.data.tag)
        })
        that.doILikeIt()
      }
    })
    wx.request({
      url: 'http://localhost:8888/comments/getCommentsByPosting',
      method: 'GET',
      data: {
        posting: app.globalData.postingId
      },
      head: {
        'content-type': 'application/json'
      },
      success:function (res) {
        that.setData({
          comments: res.data.data
        })
      }
    })
    wx.request({
      url: 'http://localhost:8888/comments/getCommentsUserHeadImg',
      method: 'GET',
      data: {
        posting: app.globalData.postingId
      },
      head: {
        'content-type': 'application/json'
      },
      success:function (res) {
        that.setData({
          commentsUserImg: res.data.data
        })
      }
    })
    wx.request({
      url: 'http://localhost:8888/comments/getCommentsUserName',
      method: 'GET',
      data: {
        posting: app.globalData.postingId
      },
      head: {
        'content-type': 'application/json'
      },
      success:function (res) {
        that.setData({
          commentsUserName: res.data.data
        })
      }
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.setData({
      loginUserInfo: app.globalData.userInfo
    })
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getPostHeadImg: function(id) {
    var that = this
    wx.request({
      url: 'http://localhost:8888/account/getAccount',
      method: 'GET',
      data: {
        id: id
      },
      head: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          postUserImg: res.data.data.headimg,
          postUserName: res.data.data.username
        })
      }
    })
  },

  preview(event) {
    wx.previewImage({
      current: this.data.post.photo[event.currentTarget.dataset.index],
      urls: this.data.post.photo,
    })
  },

  doILikeIt: function () {
    var loginAccount = app.globalData.loginAccount
    var postingId = this.data.post.id
    var that = this
    wx.request({
      url: 'http://localhost:8888/likes/doILikeIt',
      method: 'GET',
      data: {
        username: loginAccount,
        posting: postingId
      },
      head: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          doILike: res.data.data
        })
      }
    })
  },

  doLike: function(event) {
    //console.log("do like");
    var loginAccount = app.globalData.loginAccount
    var postingId = this.data.post.id
    var that = this
    wx.request({
      url: 'http://localhost:8888/likes/doLike',
      method: 'POST',
      data: {
        username: loginAccount,
        posting: postingId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        var iPost = "post.likes"
        var likes = that.data.post.likes + 1
        that.setData({
          doILike: 1,
          [iPost]: likes
        })
      }
    })
  },
  dontLike: function(event) {
    //console.log("dont like");
    var loginAccount = app.globalData.loginAccount
    var postingId = this.data.post.id
    var that = this
    wx.request({
      url: 'http://localhost:8888/likes/dontLike',
      method: 'POST',
      data: {
        username: loginAccount,
        posting: postingId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        var iPost = "post.likes"
        var likes = that.data.post.likes - 1
        that.setData({
          doILike: 0,
          [iPost]: likes
        })
      }
    })
  },

  showCommentPopup: function(event){
    this.setData({
      isCommentPopupShow: true
    })
  },

  hideCommentPopup: function(){
    this.setData({
      isCommentPopupShow: false
    })
  },

  updateCommentInputData: function(event){
    this.setData({
      commentTextareaValue: event.detail.value
    });
  },

  sendComment: function(event) {
    //console.log("do like");
    let loginAccount = app.globalData.loginAccount
    var postingId = this.data.post.id
    let content = this.data.commentTextareaValue
    var that = this
    if( content.match(/^[ ]*$/) ){
      wx.showToast({
        title: '评论不能为空',
        icon: 'error',
        duration: 1500
      });
      return;
    }
    wx.request({
      url: 'http://localhost:8888/comments/insertComments',
      method: 'POST',
      data: {
        username: loginAccount,
        posting: postingId,
        content: content
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(
          "suucessssss"
        );
        var commentVar = "post.comments";
        var commentNum = that.data.post.comments + 1;
        that.setData({
          [commentVar]: commentNum
        });
        console.log();
        that.getCommentsByGlobalPostingId(that);
        that.setData({
          commentTextareaValue: ""
        });
        that.hideCommentPopup();
        wx.showToast({
          title: '评论发送成功!',
          icon: 'success',
          duration: 2000
        });
      }
    });
    
  },

  getCommentsByGlobalPostingId: function(thisPtr){
    let that = thisPtr;
    wx.request({
      url: 'http://localhost:8888/comments/getCommentsByPosting',
      method: 'GET',
      data: {
        posting: app.globalData.postingId
      },
      head: {
        'content-type': 'application/json'
      },
      success:function (res) {
        that.setData({
          comments: res.data.data
        })
      }
    });
    wx.request({
      url: 'http://localhost:8888/comments/getCommentsUserHeadImg',
      method: 'GET',
      data: {
        posting: app.globalData.postingId
      },
      head: {
        'content-type': 'application/json'
      },
      success:function (res) {
        that.setData({
          commentsUserImg: res.data.data
        })
      }
    });
    wx.request({
      url: 'http://localhost:8888/comments/getCommentsUserName',
      method: 'GET',
      data: {
        posting: app.globalData.postingId
      },
      head: {
        'content-type': 'application/json'
      },
      success:function (res) {
        that.setData({
          commentsUserName: res.data.data
        })
      }
    });
  },

  showLoginComment() {
    wx.showToast({
      title: '登录后方可评论',
      icon: "error"
    })
  },
  showLoginLike() {
    wx.showToast({
      title: '登录后方可点赞',
      icon: "error"
    })
  },
})